package org.jiamandan.com.strategy.impl.tram.cmd;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDUpgradeDto;
import org.jiamandan.com.strategy.impl.tram.TramBasicService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-12-12 14:00
 * @功能说明： FTP下发升级

 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	        CWJ       	创建
 * @end
 */
@Service
@AllArgsConstructor
@Slf4j
public class ZDIssueUpgradeService implements TramBasicService {

  //  private FTPProperties ftpProperties;

    @Override
    public void doExecute(byte[] data, Channel client) {
        /**
         * 启动/停止升级命令   0xAA(170) 启动升级 0x55(85) 停止升级
         * FTP 服务器 IP 地址   192.168.1.100 顺序传输 0xc0 0xa8 0x01 0x64
         * FTP 服务器端口    18090
         * 用户名   ASCII
         * 密码   ASCII
         * 路径   ASCII
         * 文件名   ASCII
         * 文件大小  HEX
         * 文件校验码   HEX
         * 软件版本号   主版本号  次版本号 修订号  0x00
         */
      /*  ZDUpgradeDto dto = JSON.parseObject(CommonUtil.bytesToString(data), ZDUpgradeDto.class);
        StringBuilder builder = new StringBuilder();

        String[] ip = ftpProperties.getIp().split("\\.");
        StringBuilder ipsb = new StringBuilder();
        for (int i = 0; i < ip.length; i++) {
            String hexstr = Integer.toHexString(Integer.valueOf(ip[i]));
            ipsb.append(StringUtils.leftPad(hexstr, 2, "0"));
        }
        builder.append(Integer.toHexString(Func.toInt(dto.getFtpcmd())))
                .append(ipsb)
                .append(CommonUtil.reverseHex(StringUtils.leftPad(Integer.toHexString(ftpProperties.getPort()), 8, "0")))
                .append(StringUtils.rightPad(stringToAsciiToHex(ftpProperties.getUserName()), 32, "0"))
                .append(StringUtils.rightPad(stringToAsciiToHex(ftpProperties.getPassword()), 32, "0"))
                .append(StringUtils.rightPad(stringToAsciiToHex(dto.getPath()), 64, "0"))
                .append(StringUtils.rightPad(stringToAsciiToHex(dto.getFilename()), 64, "0"))

                .append(Integer.toHexString(dto.getFilecheckcode()))
                .append(StringUtil.rightPad(Integer.toHexString(Func.toInt(dto.getFilesize())), 8, "0"))
                .append(StringUtil.rightPad(Integer.toHexString(Func.toInt(dto.getVersion())), 8, "0"));
        String replyMsg = ZDObjectUtil.pushMessage(builder.toString(), dto.getPileNo(), BizConstant.Cmd.CMD_901);
        log.info("升级报文报文：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
        */
    }

    public static String stringToAsciiToHex(String str){
        StringBuilder hex = new StringBuilder();
        char[] chars = str.toCharArray();
        for (char aChar:chars) {
            System.out.println(aChar+":"+(int)aChar+":"+Integer.toHexString((int)aChar));
            hex.append(Integer.toHexString((int)aChar));
        }
        return hex.toString();
    }


}
